<?php
namespace console\controllers;

use Yii;
use common\models\CoinWallet;
use yii\console\Controller;

/**
 * 货币脚本控制器
 */
class CoinscriptController extends Controller
{
    /**
     * 获取钱包流水
     * @author CGA
     */
    public function actionDealflow()
    {
        set_time_limit(0);
        Yii::$app->cache->set('getDealflow', 'ending');
        $startTime = Yii::$app->formatter->asTimestamp('now');
        $status = Yii::$app->cache->get('getDealflow');
        if ($status == 'running') {
            echo "it is running" . "\r\n";
            return;
        } else {
            Yii::$app->cache->set('getDealflow', 'running');
            $result = CoinWallet::getDealflow();
            echo 'execution time:' . (Yii::$app->formatter->asTimestamp('now') - $startTime);
            Yii::$app->cache->set('getDealflow', 'ending');
        }
    }

    /**
     * 充值
     * @author CGA
     */
    public function actionRecharge()
    {
        set_time_limit(0);
        Yii::$app->cache->set('recharge', 'ending');
        $startTime = Yii::$app->formatter->asTimestamp('now');
        $status = Yii::$app->cache->get('recharge');
        if ($status == 'running') {
            echo "it is running" . "\r\n";
            return;
        } else {
            Yii::$app->cache->set('recharge', 'running');
            $result = CoinWallet::recharge();
             echo Yii::$app->formatter->asDatetime(Yii::$app->formatter->asTimestamp('now')), "\r\n";
            echo 'execution time:' . (Yii::$app->formatter->asTimestamp('now') - $startTime), "\r\n", '-----------------------------------', "\r\n";
            Yii::$app->cache->set('recharge', 'ending');
        }
    }

    /**
     * 添加充值记录
     * @param number $uid    用户编号
     * @param number $coin   货币
     * @param number $amount 数额
     */
    public function actionAdd($uid, $coin, $amount)
    {
        set_time_limit(0);
        Yii::$app->cache->set('add', 'ending');
        $startTime = Yii::$app->formatter->asTimestamp('now');
        $status = Yii::$app->cache->get('add');
        if ($status == 'running') {
            echo "it is running" . "\r\n";
            return;
        } else {
            Yii::$app->cache->set('add', 'running');
            $result = CoinWallet::AddRecord($uid, $coin, $amount);
            echo Yii::$app->formatter->asDatetime(Yii::$app->formatter->asTimestamp('now')), "\r\n";
            echo 'execution time:' . (Yii::$app->formatter->asTimestamp('now') - $startTime), "\r\n", '-----------------------------------', "\r\n";
            Yii::$app->cache->set('add', 'ending');
        }
    }
}
